-- Drop old tables

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[WtSessionEntry]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[WtSessionEntry]
go

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[WtSession]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[WtSession]
go

-- Session table

create table WtSession
(
  sessionId char(32) not null primary key clustered,
  creationTime datetime not null,
  accessTime datetime not null,
  clientIp char(15),
  clientUserAgent char(128)
)
go

create index accessIdx on WtSession(accessTime)
go
create index idAccessIdx on WtSession(sessionId, accessTime)
go

-- Session entry table

create table WtSessionEntry
(
  creationTime datetime,
  name varchar(8000) not null,
  value varbinary(8000),
  sessionId char(32) not null
    references WtSession(sessionId) on delete cascade,
  primary key clustered (sessionId, name)
)
go

create index sessionNameIdx on WtSessionEntry(sessionId, name)
go
